package com.honglv.service.impl;

import com.honglv.constant.LikeContext;
import com.honglv.context.BaseContext;
import com.honglv.exception.BaseException;
import com.honglv.mapper.AttractionsMapper;
import com.honglv.mapper.NewsMapper;
import com.honglv.mapper.RounteMapper;
import com.honglv.mapper.StrategyMapper;
import com.honglv.pojo.dto.LikeDTO;
import com.honglv.pojo.vo.LikeVO;
import com.honglv.service.LikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class LikeServiceImpl implements LikeService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RounteMapper rounteMapper;

    @Autowired
    private AttractionsMapper attractionsMapper;

    @Autowired
    private StrategyMapper strategyMapper;

    @Autowired
    private NewsMapper newsMapper;


    /**
     * Description:通过status点赞或取消点赞
     * date: 2024/03/31
     * author:王铭杰
     */
    public LikeVO like(LikeDTO likeDTO) {


        //public static final String[] likeStatus = {"rounte","attractions","strategy","news"};

        if(likeDTO.getStatus() < 0 || likeDTO.getStatus() >3){
            throw new BaseException("无该文章类型");
        }
        Long id = likeDTO.getId();
        String key = LikeContext.likeStatus[likeDTO.getStatus()] + id;
        Long userId = BaseContext.getCurrentId();
        Boolean identify = stringRedisTemplate.opsForSet().isMember(key, String.valueOf(userId));
        Long likeCount = 0L;
        if(Boolean.TRUE.equals(identify)){
            stringRedisTemplate.opsForSet().remove(key,String.valueOf(userId));
            switch (likeDTO.getStatus()){
                case 0:
                    rounteMapper.like(id, -1);
                    likeCount = rounteMapper.getLikeCount(id);
                    break;
                case 1:
                    attractionsMapper.like(id,-1);
                    likeCount = attractionsMapper.getLikeCount(id);
                    break;
                case 2:
                    strategyMapper.like(id, -1);
                    likeCount = strategyMapper.getLikeCount(id);
                    break;
                case 3:
                    newsMapper.like(id, -1);
                    likeCount = newsMapper.getLikeCount(id);
                    break;
                default:
                    throw new RuntimeException("没有这种文章类型");
            }
            if(likeCount == null){
                return null;
            }
            return new LikeVO(likeCount,0);
        }else {
            stringRedisTemplate.opsForSet().add(key, String.valueOf(userId));
            switch (likeDTO.getStatus()){
                case 0:
                    rounteMapper.like(id, 1);
                    likeCount = rounteMapper.getLikeCount(id);
                    break;
                case 1:
                    attractionsMapper.like(id, 1);
                    likeCount = attractionsMapper.getLikeCount(id);
                    break;
                case 2:
                    strategyMapper.like(id, 1);
                    likeCount = strategyMapper.getLikeCount(id);
                    break;
                case 3:
                    newsMapper.like(id, 1);
                    likeCount = newsMapper.getLikeCount(id);
                    break;
                default:
                    throw new RuntimeException("没有这种文章类型");
            }
            if(likeCount == null){
                return null;
            }
            return new LikeVO(likeCount,1);
        }
    }
}
